﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.DirectoryServices;
namespace SAP
{
    public partial class login : System.Web.UI.Page
    {
        #region Active Directory Specific

        protected string JobTitle = "";
        protected string DirectManagerID = "";
        protected string ADDistingushedName = "";
        protected string Department = "";
        protected string Email = "";

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Image1_Click(object sender, ImageClickEventArgs e)
        {

           Image myButton = (Image)Login2.FindControl("im_ProgressBar");
            myButton.Visible = true;
            //im_ProgressBar.Visible = true;

            if (this.ActiveDirectoryLogin())
            {
                Response.Redirect("Homepage.aspx");
            }
        }
        public bool ActiveDirectoryLogin()
        {
            bool smooth = true;

            DirectoryEntry CurADSystem = null;
            DirectorySearcher CurADSearcher = null;
            SearchResult CurSearchResult = null;

            string LDAP_Address = "ABEOVN.COM/CN=Users,DC=ABEOVN,DC=COM";

            if (LDAP_Address.Length > 0)
            {
                try
                {
                    CurADSystem = new DirectoryEntry(LDAP_Address, "frank", "admin@123");
                    CurADSearcher = new DirectorySearcher(CurADSystem);
                    CurADSearcher.Filter = "samaccountname=" + "frank";
                    CurADSearcher.SearchScope = SearchScope.Subtree;
                    CurSearchResult = CurADSearcher.FindOne();


                    if (CurSearchResult != null)
                    {
                        ResultPropertyCollection CurResultProperties = CurSearchResult.Properties;
                        foreach (string PropertyKey in CurResultProperties.PropertyNames)
                        {
                            switch (PropertyKey)
                            {
                                case "title":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.JobTitle += TmpCollection.ToString();
                                    }

                                    break;

                                case "manager":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.DirectManagerID += TmpCollection.ToString();
                                    }
                                    break;

                                case "distinguishedname":

                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.ADDistingushedName += TmpCollection.ToString();
                                    }
                                    break;

                                case "mail":
                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.Email += TmpCollection.ToString();
                                    }
                                    break;

                                case "department":
                                    foreach (Object TmpCollection in CurResultProperties[PropertyKey])
                                    {
                                        this.Department += TmpCollection.ToString();
                                    }
                                    break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    smooth = false;
                }
                finally
                {
                    // clean up
                    CurADSystem.Close();
                    CurADSystem.Dispose();

                    CurADSearcher.Dispose();
                    CurSearchResult = null;
                }
            }
            else
            {
                smooth = false;
            }



            return smooth;
        }
    }
}
